package com.itheima.utils;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.models.*;
import com.aliyun.sdk.service.dysmsapi20170525.*;
import darabonba.core.client.ClientOverrideConfiguration;

import java.util.concurrent.CompletableFuture;

/**
 * @Author: 放学后海堤日记
 * @Date: 2023/4/22 22:30
 * @Desc:
 */
public class SMSUtils2 {
    public static void sendMessage(String signName, String templateCode, String phoneNumbers, String param) {
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId("LTAI5tNqZMQhgjHLtSpw7tte")
                .accessKeySecret("cEv8xvdgO0BxyGCy0urJuGg4xSHzgV")
                .build());

        // Configure the Client
        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou")
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();

        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .phoneNumbers(phoneNumbers)
                .signName(signName)
                .templateParam("{\"code\":\"" + param + "\"}")
                .templateCode(templateCode)
                .build();

        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);

        try {
            Thread.sleep(1000 * 2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        client.close();
    }
}
